//
//  AddTicketPersonViewController.m
//  SXH
//
//  Created by Jack on 15/12/10.
//  Copyright © 2015年 lyss. All rights reserved.
//

#import "AddTicketPersonViewController.h"
#import "CommonService.h"
#import "LibController.h"

@interface AddTicketPersonViewController ()
@property (weak, nonatomic) IBOutlet UILabel *labelTitle;
@property (weak, nonatomic) IBOutlet UIButton *btnDelete;
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *phone;
@property (weak, nonatomic) IBOutlet UITextField *uniqueID;
@property (weak, nonatomic) IBOutlet UIButton *btnPost;

@end

@implementation AddTicketPersonViewController
@synthesize ticketPersonEntity;
@synthesize btnDelete,btnPost;
@synthesize name,phone,uniqueID,labelTitle;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //提交边框
    btnPost.layer.cornerRadius = 5.0f; //圆角半径
    btnPost.layer.masksToBounds = YES; //圆角
    btnPost.layer.borderWidth = 0.5f; //边框宽度
    btnPost.layer.borderColor = [[UIColor redColor]CGColor]; //边框颜色
    btnPost.layer.backgroundColor = [[UIColor redColor]CGColor]; //背景颜色
    
    //初始化 UITextField 控件
    if(ticketPersonEntity){
        labelTitle.text = @"编辑取票人";
        name.text = ticketPersonEntity.name;
        phone.text = ticketPersonEntity.phone;
        uniqueID.text = ticketPersonEntity.uniqueID;
        btnDelete.hidden = NO;
    }else{
        labelTitle.text = @"新增取票人";
    }

}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

//点击空白隐藏键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

-(IBAction)postTicketPerson:(id)sender{
    if(![LibController validateMobile:phone.text]){
        ALERT_ERR_MSG(@"请填写正确的手机号!");
        return;
    }
    if([name.text isEqualToString:@""]
       ||[phone.text isEqualToString:@""]){
        ALERT_ERR_MSG(@"取票人、手机号不能为空!");
        return;
    }
    if (![LibController verifyIDCardNumber:uniqueID.text]) {   //验证身份证是否真实
        ALERT_ERR_MSG(@"请填写正确的身份证号!");
        return;
    }
    UserEntity *userEntity = [UserEntity sharedInstance];
    CommonService *service = [[CommonService alloc] init];
    if(ticketPersonEntity){
        NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:
                               uniqueID.text, @"idno",
                               phone.text, @"phone",
                               name.text, @"nickname",
                               userEntity.userId, @"user_id",
                               ticketPersonEntity.ID,@"id",
                               nil];
        [service getNetWorkData:param
                           Path:TICKET_PERSON_PUT
                      Successed:^(id entity) {
                          NSDictionary *dic = (NSDictionary *)entity;
                          NSNumber *code = (NSNumber*)[dic objectForKey:@"code"];
                          dispatch_async(dispatch_get_main_queue(), ^{
                              if([code intValue] == 0){
                                  [_ticketPersonView viewDidLoad];
                                  //返回上一页
                                  [self.navigationController popViewControllerAnimated:YES];
                              }
                          });
                          
                      } Failed:^(NSInteger errorCode, NSString *message) {
                          DebugLog(@"%@", message);
                      }];
    }else{
        NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:
                               userEntity.userId, @"user_id",
                               name.text, @"nickname",
                               phone.text, @"phone",
                               uniqueID.text, @"idno",
                               nil];
        [service getNetWorkData:param
                           Path:TICKET_PERSON_POST
                      Successed:^(id entity) {
                          NSDictionary *dic = (NSDictionary *)entity;
                          NSNumber *code = (NSNumber*)[dic objectForKey:@"code"];
                          dispatch_async(dispatch_get_main_queue(), ^{
                              if([code intValue] == 0){
                                  [_ticketPersonView viewDidLoad];
                                  //返回上一页
                                  [self.navigationController popViewControllerAnimated:YES];
                              }
                          });
                          
                      } Failed:^(NSInteger errorCode, NSString *message) {
                          DebugLog(@"%@", message);
                      }];
    }
}

-(IBAction)btnDelAction:(id)sender{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"删除取票人" message:@"是否要删除该取票人？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alert.tag = 90010;
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 90010) {
        if (buttonIndex == 1) {
            UserEntity *userEntity = [UserEntity sharedInstance];
            CommonService *service = [[CommonService alloc] init];
            NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:
                                   ticketPersonEntity.ID,@"ticket_person_id",userEntity.userId, @"user_id",nil];
            [service getNetWorkData:param
                               Path:TICKET_PERSON_DELETE
                          Successed:^(id entity) {
                              NSDictionary *dic = (NSDictionary *)entity;
                              NSString *code = [dic objectForKey:@"code"];
                              dispatch_async(dispatch_get_main_queue(), ^{
                                  if([code intValue]==0){
                                      [_ticketPersonView viewDidLoad];
                                      [self.navigationController popViewControllerAnimated:YES];
                                  }
                              });
                          } Failed:^(NSInteger errorCode, NSString *message) {
                              DebugLog(@"%@", message);
                          }];
        }
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"AddTicketPersonViewController"];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"AddTicketPersonViewController"];
}

@end
